/*
	邮件模板列表
	部分系统邮件模板 无法删除

	系统邮件模板：
		验证码邮件模板
		VIP等级晋升邮件模板
		新订单邮件模板
		ecpay退款通知邮件
		客诉反馈邮件模板（收到客诉）
*/

var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');
var DICT_ADMIN = JSON.parse(sessionStorage.getItem('kplus_web_dict_admin'));

function DomInit(){

	var ajaxData = { aId: adminLoginedInfo.aId, aToken: adminLoginedInfo.aToken };
	
	var initTable = function(data, table){
		$('#dataSize').text(data.length);

		var tableData = [];
		for (var i = data.length - 1; i >= 0; i--) {
			tableData.push({
				id: data[i].model_id,
				name: data[i].name || '',
				subject: data[i].subject || '',
				html: data[i].html || '',
				admin: DICT_ADMIN[data[i].a_id] || ''
			});
		}
		table.render({
			elem: '#tableContent',
			limit: 30, //分页
			height: 680,
			page: true,
			cols:[[ //表头
				{field: 'id', title: '模板编号', sort: true, fixed: 'left', width:120},
				{field: 'name', title: '模板名称'},
				{field: 'subject', title: '邮件标题'},
				{field: 'html', title: '邮件内容'},
				{field: 'admin', title: '创建管理员'},
				{field: 'option', title: '操作', toolbar:'#toolbar'}
			]],
			data: tableData
		});
	}

	this.getEmailModel = function(layer, form, table){
		/*
			@获取模板列表
		*/
		var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
		var _ajaxData = ajaxData;

		$.ajax({
			url: BASE_SERVICEURL + URlS.sys_email,
			type: 'GET',
			data: _ajaxData,
			success: function(responseData){
				layer.close(loadIndex);
				if(responseData.msg.code == 200){
					//console.log(responseData.data)
					initTable(responseData.data, table)
					//initForm(responseData.data.data);
				}else{
					layer.msg(responseData.msg.msg, {time: 2000})
				}
			},
			error: function(responseData){
				layer.close(loadIndex);
				layer.msg(MESSAGES.service_error, {time: 2000});
			}
		});
	};
}


$(function(){
	layui.use(['layer', 'form', 'table'], function(){
		var layer = layui.layer, form = layui.form, table = layui.table;
		dom_init = new DomInit();
		dom_init.getEmailModel(layer, form, table)

		//详情查看
		table.on('tool(email-list-table)', function(obj){
			var data = obj.data;   //本行数据
			var layEvent = obj.event; 
			var tr = obj.tr;  //当前行
			//console.log(data)
			if(layEvent == 'seeDetatil'){  //查看详情
				x_admin_show('查看詳情', 'email-detail.html?model_id='+data.id+'', 1200, 600);
			};
		});
	})
})


















